<!-- 排班设置页 -->
<template>
  <div class="dashboard-container waybill-list customer-list-box">
    <div class="app-container">
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
      >
        <el-card
          class="table-card-box"
          shadow="never"
        >
          <el-button
            type="warning"
            style="padding: 9px 0px; margin-bottom: 20px; width: 120px;"
            @click="handleWorkPattern()"
          >添加工作模式</el-button>
          <el-table
            :data="workPatternList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <div
              v-show="
                (!workPatternList || workPatternList.length <= 0) &&
                  !listLoading
              "
              slot="empty"
            >
              <img
                src="@/icons/pic-kong.png"
                alt="img"
                style="margin-top: 20px; width: 25%; height: 25%"
              />
              <p style="margin-top: -20px; padding-bottom: 0px">这里空空如也</p>
            </div>
            <el-card
              v-show="
                (!workPatternList || workPatternList.length <= 0) && !listLoading
              "
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column
              align="left"
              label="工作模式编号"
              prop="id"
              min-width="189"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="工作模式"
              prop="name"
              min-width="188"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="类型"
              prop="workPatternTypeDesc"
              min-width="188"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="工作日期"
              prop="workDate"
              min-width="188"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="工作时间"
              prop="workTime"
              min-width="214"
            >
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              fixed="right"
              width="165"
              class-name="small-padding fixed-width"
            >
              <template v-slot="{row}">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="handleWorkPattern(row.id)"
                >编辑</el-link>
                <el-divider direction="vertical"></el-divider>
                <el-link
                  type="danger"
                  :underline="false"
                  @click="deleteWorkHistory(row.id)"
                >删除</el-link>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div
            v-if="total > 10"
            class="pagination"
          >
            <div class="pages">
              <el-pagination
                :current-page="searchData.page"
                :total="+total"
                :page-size="searchData.pageSize"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="sizeChange"
                @current-change="currentChange"
              />
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <WorkPatternDialog
      ref="workPatternDialog"
      class="add-form-dialog"
      :current-node-id="currentNodeId"
      @updateWorkPatternList="getList"
    />
  </div>
</template>

<script>
import { workHistoryList, deleteWorkHistory } from '@/api/transit'
import WorkPatternDialog from './components/work-pattern-add.vue'
export default {
  components: {
    WorkPatternDialog
  },
  data() {
    return {
      listLoading: false,
      searchData: {
        page: 1,
        pageSize: 10
      },
      total: 0,
      workPatternList: [],
      currentNodeId: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { data } = await workHistoryList(this.searchData)
      this.workPatternList = data.items
      this.workPatternList.forEach((item) => {
        item.workTime =
          ((item.workEndMinute1 - item.workStartMinute1) / 60).toFixed(2) +
          '小时'
      })
      this.total = data.counts
      this.listLoading = false
    },
    sizeChange(val) {
      this.searchData.pageSize = val
      this.getList()
    },
    currentChange(val) {
      this.searchData.page = val
      this.getList()
    },
    handleWorkPattern(id) {
      if (id) {
        this.currentNodeId = id
        this.$nextTick(() => {
          this.$refs.workPatternDialog.getWorkPatternDetail()
          this.$refs.workPatternDialog.openDialog()
        })
      } else {
        this.$refs.workPatternDialog.openDialog()
      }
    },
    async deleteWorkHistory(id) {
      this.$confirm('确定要删除吗?', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteWorkHistory(id)
          if (res.code === 200) {
            this.$message.success('删除工作模式成功')
            this.getList()
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.customer-list-box {
  ::v-deep .table-card-box {
    .el-card__body {
      padding-bottom: 28px;
    }
  }
}
::v-deep .pagination {
  padding-bottom: 40px;
}
::v-deep .el-dialog__body {
  padding-top: 30px !important;
  .el-tabs__header {
    padding-bottom: 25px;
  }
}
::v-deep .el-tabs__nav,
.is-top {
  transform: translateX(30px) !important;
}
</style>
